package datastructure.array;

import java.util.Arrays;

/**
 * @author XY
 * @version 1.0
 * @date 2022/3/8 9:13
 * @Description
 */
public class Leet136 {

    public static int singleNumber(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            boolean flag = false;
            for (int j = 0; j < nums.length; j++) {
                if(j!=i&&nums[j]==nums[i]){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                return nums[i];
            }
        }
        return -1;
    }

    public static int singleNumber1(int[] nums) {
        if(nums.length==1){
            return nums[0];
        }
        Arrays.sort(nums);
        if(nums[nums.length-1]!=nums[nums.length-2]){
            return nums[nums.length-1];
        }
        for (int i = 0; i < nums.length-1; i++) {
            if(i%2==0&&nums[i]!=nums[i+1]){
                return nums[i];
            }
        }
        return -1;
    }

    //异或运算
    public static int singleNumber2(int[] nums) {
        int ans = 0;
        for(int num: nums) {
            ans ^= num;
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {2,2,1};
        System.out.println(0%2);
        System.out.println(singleNumber1(arr));
    }
}
